import 'package:flutter/material.dart';
import 'ClientModel.dart';
import 'Database.dart';
import 'dart:math' as math;
import 'package:path/path.dart' as p;
import 'dart:io';
import 'package:excel/excel.dart';
//void main() => runApp(MaterialApp(home: SqliteApp()));

class SqliteApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<SqliteApp> {
  // data for testing
  var rows=[TableRow(
              decoration: BoxDecoration(color: Colors.grey),
              children: 
                [
                  Text("id"),
                  Text("bh"),
                  Text("op"),
                ]
              )];
  List<Client> testClients = [
    Client(1, "Raouf",   false),
    Client(2, "Zaki",   true),
    Client(3, "oussama",  false),
  ];
  void newData(){
    Client n=Client(1, "Raouf",  false);
    DBProvider.db.newClient(n);
  }
  void getData() async{
    String file = p.join(p.current, "1111.xlsx");
    print(file);
    var bytes = File(file).readAsBytesSync();
    // print(bytes);
    var excel = Excel.decodeBytes(bytes);
    print(excel);
    // for (var table in excel.tables.keys) {
    //   print(table); //sheet Name
    //   // print(excel.tables[table].maxCols);
    //   // print(excel.tables[table].maxRows);
    //   // for (var row in excel.tables[table].rows) {
    //   //   print('$row');
    //   // }
    // }
    // List<Client> data=await DBProvider.db.getAllClients();
    // print("data=========");
    // print(data);
    // var newRows=<TableRow>[];
    //     newRows.add(TableRow(
    //           decoration: BoxDecoration(color: Colors.grey[200]),
    //           children: 
    //             [
    //               Text("id"),
    //               Text("Text"),
    //               Text("completed"),
    //               Text("op"),
    //             ]
    //           ));
    //     for(var i=0;i<data.length;i++){
    //       newRows.add(TableRow(
    //           children: ([
    //             Text(data[i].id.toString()),
    //             Text(data[i].text),
    //             Text(data[i].completed.toString()),
    //             Row(
    //               children: [new MaterialButton(
    //               child: new Text("edit"),
    //               color: Colors.greenAccent,
    //               onPressed: (){},
    //             ),
    //             new MaterialButton(
    //               child: new Text("delete"),
    //               color: Colors.greenAccent,
    //               onPressed: (){},
    //             )
    //             ])
    //           ]),
    //         ));
    //     }
    //     setState((){
    //       rows=newRows;
    //     });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Flutter SQLite")),
      body: ListView(
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
              ElevatedButton(
                onPressed: (){
                  //Navigator.of(context).pop();

                }, child: Text("Back")),
              ElevatedButton(
              child: Text("load"),
              onPressed: getData
              ),
              ElevatedButton(
                child: Text("new"),
                onPressed: newData
              ),]
            ),
            Table(
              border: TableBorder.all(),
              defaultVerticalAlignment: TableCellVerticalAlignment.middle,
              children: rows,
            ),
            ]),
      //FutureBuilder<List<Client>>(
      //  future: DBProvider.db.getAllClients(),
      //  builder: (BuildContext context, AsyncSnapshot<List<Client>> snapshot) {
      //    if (snapshot.hasData) {
      //      List<Client> data=snapshot.data as List<Client>;
      //      //int length=0;            
      //      //if (snapshot.data!=null){
      //      //  length=snapshot.data.length;
      //      //}
      //      return ListView.builder(
      //        itemCount: data.length,
      //        itemBuilder: (BuildContext context, int index) {
      //          Client item = data[index];
      //          return Dismissible(
      //            key: UniqueKey(),
      //            background: Container(color: Colors.red),
      //            onDismissed: (direction) {
      //              DBProvider.db.deleteClient(item.id);
      //            },
      //            child: ListTile(
      //              title: Text(item.text),
      //              leading: Text(item.id.toString()),
      //              trailing: Checkbox(
      //                onChanged: (bool? value) {
      //                  DBProvider.db.blockOrUnblock(item);
      //                  setState(() {});
      //                },
      //                value: item.completed,
      //              ),
      //            ),
      //          );
      //        },
      //      );
      //    } else {
      //      return Center(child: CircularProgressIndicator());
      //    }
      //  },
      //),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () async {
          print(testClients.length);
          var at=math.Random().nextInt(testClients.length);
          print(at);
          Client rnd = testClients[at];
          await DBProvider.db.newClient(rnd);
          setState(() {});
        },
      ),
    );
  }
}
